// pages/login/login.js
var util = require('../../network_util.js');
var commonjs = require('../../common.js');
var extraLine = [];
const requestUrl = require('../../config').loginCode
const loginUrl = require('../../config').login
const duration = 2000;
const codeDuration = 60;
var app = getApp();
// var phone = "";
// var vCode = "";
// 从从60到到0倒计时  
function countdown(that) {
  var second = that.data.second
  if(second == 0){
    that.setData({ second: codeDuration})
    if (commonjs.validateMobile(that.data.phone)) {
      that.setData({ codeStatus: 1 })
    }
    return;
  }
  // if (second == 0) {
  //   that.setData({
  //     second: "发送验证码"
  //   });
  //   return;
  // }
  var time = setTimeout(function () {
      // console.log(that.data.second);
    if(that.data.second > 0){
      that.setData({
        second: second - 1
      });
    }
    countdown(that);
  }
    , 1000)
}

Page({

  /**
   * 页面的初始数据
   */
  data: {
    btnOpacity: 0.5,
    // text: initData,
    phone: '',//1519823072',
    vCode: "",
    getCodeTitle: '获取验证码',
    second: codeDuration,
    codeStatus: 0, //0-手机号未填好，1-手机号已填可以发送验证码，2-验证码已发送，数秒
    loginStatus: 0, //0-没填验证码，灰掉； 1-可以登录了
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(this.data.codeStatus);
    if (app.globalData.userInfo){
      wx.redirectTo({
        url: '../placing/pickup',
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  phoneBlur: function(e){
    // var rdm = commonjs.getRandomString(9);
    var val = e.detail.value;
    // console.log(val);
    //如果位数对了
    if(val.length == 11){
      //检查是否是正确的手机格式
      if (commonjs.validateMobile(val)) {
      // if (this.valMobile(val)){
        console.log("right:" + val);
        this.setData({ phone: val});
        this.setData({codeStatus: 1}); //改变“获取验证码”按钮状态
      }else{
        //红条警告：
        console.log("wrong:" + val);
        this.setData({ loginStatus: 0 });
        this.setData({ codeStatus: 0 }); //改变“获取验证码”按钮状态
        wx.showToast({
          title: '请输入正确的手机号码',
        })
      }
    }else{
      this.setData({ loginStatus: 0 });
      this.setData({ codeStatus: 0 }); //改变“获取验证码”按钮状态
    }
  },
  vCodeBlur: function (e) {
    // console.log(e.detail.value);
    this.setData({vCode: e.detail.value});
    // console.log(vCode.length);
    if(this.data.vCode.length>=4 && commonjs.validateMobile(this.data.phone)){
      this.setData({ loginStatus: 1 });
    }else{
      this.setData({ loginStatus: 0 });
    }
  },
  getCode: function(e){
    console.log(this.data.phone);
    this.setData({codeStatus: 2})
    var that = this;
    countdown(this);
    wx.request({
      url: requestUrl,
      data:{phone: that.data.phone, type: 0},
      success: function (res) {
        // console.log(res);
        if(res.data.code == 200){
          wx.showModal({
            title: '成功',
            // content: '发送验证码成功！' + res.data.data,
            content: '发送验证码成功！',
            showCancel: false
            });
            // that.setData({vCode: res.data.data});
            console.log(res.data.data);        
        }else{
          wx.showModal({
            title: '发送失败',
            content: res.data.msg,
            showCancel: false
          })
        }
        // console.log(res);
      },
      fail: function(res){
        console.log(res);
      }
    })
  },
  onLogin: function(){
    console.log('login in..');
    var that = this;
    wx.request({
      url: loginUrl,
      method: "POST",
      data: { phone: that.data.phone, vCode: that.data.vCode},
      success: function(res){
        console.log(res);
        if(res.data.code == 200){
          //登陆成功，跳转
          wx.setStorageSync("userInfo", res.data.data);
          app.globalData.userInfo = res.data.data;
          wx.redirectTo({
            url: '../placing/pickup',
          })
          // wx.navigateBack();
        }else{
          wx.showModal({
            title: '登录失败',
            content: res.data.msg,
            showCancel: false
          })
        }
      },
      fail: function(res){
        console.log(res);
      }
    })
  }
})